(function(Jml, ElementFactory) {

	ElementFactory.addView("form", {
		_buildConfig: function() {
			var form = this,
				config = form.config;
			config.prop["class"] = (config.prop["class"] || "") + "ccay-form";
			this._bulidOperation();
		},

		getOperationMapping: function() {
			return {

				getManager: function() {

				},
				reset: function() {
					this.getManager().reset();
				},
				save: function(ajaxOp) {
					this.getManager().save(ajaxOp);
				},
				reload: function(urlOrAjax) {
					this.getManager().reload(urlOrAjax);
				}

			}
		},
		_compileChildren: function() {
			var form = this,
				layoutOp = form.config.prop,
				layout = Jml.ElementLayout.build(layoutOp.layout || "form-Flow", layoutOp, form.builder),
				itemType, defaultType = "form-item",
				item, itemOp,
				itemConfig = new Jml.ConfigLoader(null, form.page);

			//布局开始
			layout.start(form.config.htmlAttr);
			//添加子项的验证初始化代理，将子项在option中配置的验证规则收集到form的option中
			form.page.fnBeforeLoad.add(function() {
				//得到form的option并且初始化验证使用的rules规则对象
				form.config.execute();
				//得到form的option并且初始化验证使用的rules规则对象
				var formOp = form.config.option;
				if (!formOp.fields) {
					formOp.fields = {};
				}
				form.page.addInitHandler(form, function(elem) {
					var option = elem.config.option || {};
					//如果子项有配置验证规则rule，则添加
					if (option.rule) {
						formOp.fields[option.field || elem.domTemplate.prop("name")] = option.rule;
					}
				});
			});
			//添加子项
			this.domTemplate.children().each(function(i) {
				item = $(this);
				itemConfig.load(item).execute();
				itemOp = itemConfig.option;
				itemType = itemOp.itemType;

				if (itemType == 'custom') {
					layout.endPart(itemOp);
					Jml.compileChildNodes(form, item);
					return;
				} else if (itemType == 'toolbar' || item.attr("data-role") == "toolbar") {
					layout.endPart(itemOp);
					form.builder.pushHtml('<div class="ccay-operate">');
					Jml.compileChildNodes(form, item);
					form.builder.pushHtml('</div>');
				} else {
					layout.startItem(itemOp);
					Jml.compileChildNodes(form, item);
					layout.endItem(itemOp);
				}
			});
			layout.end(layoutOp);
			//form执行完毕，需要清理掉之前添加的InitHandler			
			form.page.fnBeforeLoad.add(function() {
				form.page.removeInitHandler(form);
				form.page.findElement(form.config.jid).iForm(form.config.option);
			});
		},
		_bulidOperation: function() {
			var form = this,
				option = this.config.option,
				operation = option.operation;
			if (operation && operation.fill) {
				form.page.onLoad.add(function() {
					operation.fill.success = function(data) {
						option.data = data;
					}
					Ccay.Core.ajax(operation.fill);
				})
			}
		}
	});


	// by lixiao 175152
	ElementFactory.addControl("dialog", {
		// init阶段，隐藏需弹出的内容块
		_buildConfig: function() {
			var config = this.config;
			config.prop["style"] = (config.prop["style"] || "") + ";display:none";
		},
		// fnBeforeLoad阶段，初始化
		_initControl: function(target, op) {
			// target定义需要绑定点击事件的button等
			$(op.trigger).bind("click", target, function(e) {
				// 如果定义了url就已url为弹出框内容，否则以自定义的标签为内容
				if (op.url) {
					Ccay.UI.dialog(op.url, op.title, op);
				} else {
					e.data.dialog(op).css("display", "block");
				}
			});
		}
	});



	// by lixiao 175152
	ElementFactory.addControl("tab", {
		// 为每个子元素添加html标签
		_buildConfig: function() {
			// 用来将data-option里的字符串变成键值对象
			var itemConfig = new Jml.ConfigLoader(null, this.page);
			this.domTemplate.children().each(function() {
				var item = $(this);
				itemConfig.load(item).execute();
				item.attr({
					"tabid": itemConfig.option.tabid,
					"url": itemConfig.option.url,
					"title": itemConfig.option.title
				});
			});
		},
		controlName: "ccayTab"
	});

	ElementFactory.addControl("label", {
		template: "<label ${prop} > </label>",
		// 为每个子元素添加html标签
		_initControl: function(target, op) {
			target.addClass(op.addClass);
			// 用来将data-option里的字符串变成键值对象
			if (op.i18n) {
				target.attr({
					"zh_CN": op.cn.text,
					"en_US": op.us.text
				});
				target.addClass("i18n");
				target.ccayI18n();
			} else {
				// 如果国际化为false，则优先看是否有中文，没有就取英文
				if (op.cn.text && op.cn.text.length > 0) {
					target.text(op.cn.text);
				} else if (op.us.text && op.us.text.length > 0) {
					target.text(op.us.text);
				}

			}
		}
	});

	ElementFactory.addControl("tree", {
		template: "<ul ${prop} ></ul>",
		_initControl: function(target, op) {
			var dataOrUrl, callback;
			if (op.dataOrUrl) {
				dataOrUrl = op.dataOrUrl;
				delete op.dataOrUrl;
			}
			if (op.callback) {
				callback = op.callback;
				delete op.callback;
			}
			target.ccayTree(dataOrUrl, op, callback);
		}
	});

	/**
	 * 注册toolbar类型控件
	 */
	ElementFactory.addControl("toolbar", {
		_build: function() {
			var elem = this,
				config = elem.config,
				builder = elem.builder;

			config.buildHtmlAttr();
			config.execute();
			var option = config.option;
			var flowClass = '';
			option.flow && (flowClass = ' toolbar-flow');

			var tagName = elem.domTemplate[0].tagName;
			builder.pushHtml('<div class="ccay-toolbar' + flowClass + '" ' + config.htmlAttr.prop + ' >');
			Jml.compileChildNodes(elem, elem.domTemplate);
			builder.pushHtml('</div>');
		}
	});

})(Ccay.Jml, Ccay.Jml.ElementFactory);